package servlet;

import entity.Person;
import entity.PersonService;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import util.LoginManager;
import util.Utilities;

/**
 *
 * @author HOME
 * @version
 */
public class LoginServlet extends HttpServlet {

    /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String loginname = request.getParameter("name");
        String pwd = request.getParameter("password");
        String remember = request.getParameter("remember");
        String go = (String) request.getParameter("go");

        if(Utilities.isValid(go)) {
            go = response.encodeRedirectURL(go);
        }else {
            go = "";
        }

        String forward = "login.jsp?msg=error&go=" + go;
        HttpSession session = request.getSession(true);
        Integer logincount = (Integer) session.getAttribute("logincount");
        if (logincount == null) {
            logincount = 0;
        }
        if (logincount > 3) {
            forward = "Verification.jsp?go=" + go;
        } else if (Utilities.isValid(loginname) && Utilities.isValid(pwd)) {
            //TODO: check for unactivation account, resend activation letter if necessary.
            Person person = PersonService.login(loginname, pwd);
            if (person == null) {
                logincount += 1;
                session.setAttribute("logincount", logincount);
                if (logincount > 3) {
                    forward = "Verification.jsp?go=" + go;
                } else {
                    forward = "login.jsp?msg=error&go=" + go;
                }
            } else {
                Cookie loginCookie = new Cookie("loginname", loginname);
                //todo: add MD5 login hash cookies later
                loginCookie.setMaxAge(-1);
                //loginCookie.setSecure(true); //todo:SSL
                //TODO: implement the remember me function
                if (remember != null) {
                    loginCookie.setMaxAge(2592000); // 30 days
                }
                response.addCookie(loginCookie);
                LoginManager.login(session, loginname, person.getPersonID());               
                if (Utilities.isValid(go)) {
                    forward = go;
                } else {
                    forward = this.getServletContext().getContextPath();//main page
                }
            }
        }
        response.sendRedirect(forward);
        return;
    }
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** Returns a short description of the servlet.
     */
    public String getServletInfo() {
        return "Short description";
    }
    // </editor-fold>
}
